<?php
/**
 * Created by PhpStorm.
 * User: Sky
 * Date: 17/4/25
 * Time: 下午1:49
 */
$this->title = '菜单管理';

?>
<script type="text/javascript">

    $(function ($) {
        $('#myModalEdit').modal({
            keyboard: false, backdrop: 'static', show: false
        });
    })

    function filter() {
        window.location.reload();
    }


    function onAddData(is_add, id) {
        $("#myModalEdit").modal('show');
        var data = {};
        if (is_add) {
            data.is_add = is_add;
        }
        if (id != undefined) {
            data.id = id;
        }

        modalEdit(data);
    }

    //打开新增修改页面
    function modalEdit(data) {
        $("#modaledit").html('正在加载数据...');

        requestHtml('menu-detail-partial', data, true, function (responseHtml) {
            $("#modaledit").html(responseHtml);
        });
    }

    function saveData() {
        var id = $("#menu_submit_form").find("#id").val();
        var parent_id = $("#menu_submit_form").find("#parent_id").val().trim();
        var name = $("#menu_submit_form").find("#name").val().trim();
        var controller = $("#menu_submit_form").find("#controller").val().trim();
        var action = $("#menu_submit_form").find("#action").val().trim();
        var icon = $("#menu_submit_form").find("#icon").val().trim();
        var sort = $("#menu_submit_form").find("#sort").val().trim();
        var is_enabled = 0;
        if ($("#menu_submit_form").find("#is_enabled").is(':checked')) {
            is_enabled = 1;
        }


        if (name == '' || name == null) {
            errorDialog('菜单名称不能为空');
            return false;
        }

        if (sort == '' || sort == null) {
            errorDialog('排序号不能为空');
            return false;
        }

        if (isNaN(sort)) {
            errorDialog('排序号必须为数字');
            return false;
        }

        if (validateLength(sort, '排序号', 1, 8) === false) {
            return false;
        }

        if (validateLength(controller, 'controller', 1, 64, 1) === false) {
            return false;
        }

        if (validateLength(action, 'action', 1, 64, 1) === false) {
            return false;
        }

        var parent_name = $("#menu_submit_form").find("#parent_name").val().trim();

        if(parent_name != '系统菜单'){
            if (controller == '' || controller == null) {
                errorDialog('非一级菜单controller不能为空');
                return false;
            }
        }

        var data = {};
        data.id = id;
        data.parent_id = parent_id;
        data.name = name;
        data.controller = controller;
        data.action = action;
        data.icon = icon;
        data.sort = sort;
        data.is_enabled = is_enabled;

        requestJson("menu-save-handler", data, true, function (response) {
            if (response.status == 0) {
                filter();
            } else {
                errorDialog(response.message);
            }
        });
    }


    function deleteMenu(id) {
        confirmDialog(id, null, function (id) {
            var data = {};
            data.id = id;
            requestJson("menu-delete-handler", data, true, function (response) {
                if (response.status == 0) {
                    filter();
                } else {
                    errorDialog(response.message);
                }
            });
        });
    }

</script>

<div class="main-title">
    <div class="title-txt"><?= $this->title ?></div>
</div>


<!--列表-->
<div class="panel panel-frame">
    <div class=" table-responsive">
        <table class="table table-bordered table-hover">
            <thead>
            <tr>
                <th>菜单名称</th>
                <th>controller</th>
                <th>action</th>
                <th>icon</th>
                <th>是否启用</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>

            <tr>
                <td style="text-align: left">系统菜单</td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td class="table-text-align-left">
                    <a class="btn btn-link" href="javascript:onAddData(true)">添加子菜单</a>
                </td>
            </tr>
            <?php foreach ($menus as $menu): ?>
                <tr>
                    <td style="text-align: left;padding-left: <?= $menu['level'] * 35 ?>px"><i
                                class="tier-icon"></i><?= $menu['name'] ?></td>
                    <td><?= $menu['controller'] ?></td>
                    <td><?= $menu['action'] ?></td>
                    <td><?= $menu['icon'] ?></td>
                    <td>
                        <span class="label label-<?= $menu['is_enabled'] == 1 ? 'success' : 'danger' ?>">
                            <?= $menu['is_enabled'] == 1 ? '已启用' : '已停用' ?>
                        </span>
                    </td>
                    <td class="table-text-align-left">
                        <a class="btn btn-link" href="javascript:onAddData(false,'<?= $menu['id'] ?>')">编辑</a>
                        <a class="btn btn-link" href="javascript:deleteMenu('<?= $menu['id'] ?>')">删除</a>
                        <a class="btn btn-link" href="javascript:onAddData(true,'<?= $menu['id'] ?>')">添加子菜单</a>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>


<!--edit-->
<div class="modal fade" id="myModalEdit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">菜单管理</h4>
            </div>
            <div id="modaledit" class="modal-body">
            </div>
            <div class="modal-footer">
                <a class="btn btn-primary" onclick="saveData()"><i class="fa fa-save"></i> 保存
                </a>
                <a class="btn btn-cancel" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </a>
            </div>
        </div>
    </div>
</div>
